/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.sys.entity;

import java.util.Date;

import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Length;

import cn.net.ecode.common.persistence.TreeEntity;
import cn.net.ecode.common.persistence.annotation.Id;
import cn.net.ecode.modules.sys.utils.UserUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 机构Entity
 * @author ThinkGem
 * @version 2014-8-19
 */
public class Office extends TreeEntity<Office> {

	private static final long serialVersionUID = 1L;
	private String officeCode; // 机构编码（唯一编码）
	private String officeName; // 机构名称
	private String viewCode;  // 显示的代码名称（仅作为显示用，集团内唯一）
	private String fullName; // 名称全称
	private String type; // 机构类型（1：公司；2：部门；3：小组）
	private String address; // 联系地址
	private String zipCode; // 邮政编码
	private String leader; // 负责人
	private String phone; // 电话
	private String fax; // 传真
	private String email; // 邮箱
	private String extendS1; // 扩展字段String1
	private String extendS2; // 扩展字段String2
	private String extendS3; // 扩展字段String3
	private String extendS4; // 扩展字段String4
	private String extendS5; // 扩展字段String5
	private Long extendI1; // 扩展字段Integer1
	private Long extendI2; // 扩展字段Integer2
	private Long extendI3; // 扩展字段Integer3
	private Float extendF1; // 扩展字段Float1
	private Float extendF2; // 扩展字段Float2
	private Float extendF3; // 扩展字段Float3
	private Date extendD1; // 扩展字段Date1
	private Date extendD2; // 扩展字段Date2
	private Date extendD3; // 扩展字段Date3

	private Company company; // 根据公司查询机构，组织机构所属公司
	
//	private String userCode;	// 根据用户编号查询授权的机构列表
	
//	private String grade; 	// 机构等级（1：一级；2：二级；3：三级；4：四级）
//	private String master; 	// 负责人
//	private String useable;//是否可用
//	private User primaryPerson;//主负责人
//	private User deputyPerson;//副负责人
//	private List<String> childDeptList;//快速添加子部门

	public Office() {
		super();
		this.type = TYPE_DEPT;
	}

	public Office(String id) {
		super(id);
	}

	public Office(Company company) {
		this();
		this.company = company;
	}

//	public Office(User user){
//		this();
//		if (user != null){
//			this.userCode = user.getUserCode();
//		}
//	}

	public Office getParent() {
		return parent;
	}

	public void setParent(Office parent) {
		this.parent = parent;
	}

	@Id
	public String getOfficeCode() {
		return officeCode;
	}

	public void setOfficeCode(String officeCode) {
		this.officeCode = officeCode;
	}

	@Length(min = 1, max = 100, message = "机构名称长度不能超过 100 个字符")
	public String getOfficeName() {
		return officeName;
	}

	public void setOfficeName(String officeName) {
		this.officeName = officeName;
	}

	@Pattern(regexp="[a-zA-Z0-9_]{0,30}", message="编码长度不能大于 30 个字符，并且只能包含字母、数字、下划线")
	public String getViewCode() {
		return viewCode;
	}

	public void setViewCode(String viewCode) {
		this.viewCode = viewCode;
	}

	@Length(min = 0, max = 200, message = "名称全称长度不能超过 200 个字符")
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	
	@Length(min = 1, max = 1)
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Length(min = 0, max = 255)
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Length(min = 0, max = 100)
	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	@Length(min = 0, max = 100, message = "负责人长度不能超过 100 个字符")
	public String getLeader() {
		return leader;
	}

	public void setLeader(String leader) {
		this.leader = leader;
	}
	
	@Length(min = 0, max = 200)
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Length(min = 0, max = 200)
	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	@Length(min = 0, max = 200)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	@Length(min = 0, max = 500, message = "扩展字段String1长度不能超过 500 个字符")
	public String getExtendS1() {
		return extendS1;
	}

	public void setExtendS1(String extendS1) {
		this.extendS1 = extendS1;
	}

	@Length(min = 0, max = 500, message = "扩展字段String2长度不能超过 500 个字符")
	public String getExtendS2() {
		return extendS2;
	}

	public void setExtendS2(String extendS2) {
		this.extendS2 = extendS2;
	}

	@Length(min = 0, max = 500, message = "扩展字段String3长度不能超过 500 个字符")
	public String getExtendS3() {
		return extendS3;
	}

	public void setExtendS3(String extendS3) {
		this.extendS3 = extendS3;
	}

	@Length(min = 0, max = 500, message = "扩展字段String4长度不能超过 500 个字符")
	public String getExtendS4() {
		return extendS4;
	}

	public void setExtendS4(String extendS4) {
		this.extendS4 = extendS4;
	}

	@Length(min = 0, max = 500, message = "扩展字段String5长度不能超过 500 个字符")
	public String getExtendS5() {
		return extendS5;
	}

	public void setExtendS5(String extendS5) {
		this.extendS5 = extendS5;
	}

	public Long getExtendI1() {
		return extendI1;
	}

	public void setExtendI1(Long extendI1) {
		this.extendI1 = extendI1;
	}

	public Long getExtendI2() {
		return extendI2;
	}

	public void setExtendI2(Long extendI2) {
		this.extendI2 = extendI2;
	}

	public Long getExtendI3() {
		return extendI3;
	}

	public void setExtendI3(Long extendI3) {
		this.extendI3 = extendI3;
	}

	public Float getExtendF1() {
		return extendF1;
	}

	public void setExtendF1(Float extendF1) {
		this.extendF1 = extendF1;
	}

	public Float getExtendF2() {
		return extendF2;
	}

	public void setExtendF2(Float extendF2) {
		this.extendF2 = extendF2;
	}

	public Float getExtendF3() {
		return extendF3;
	}

	public void setExtendF3(Float extendF3) {
		this.extendF3 = extendF3;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getExtendD1() {
		return extendD1;
	}

	public void setExtendD1(Date extendD1) {
		this.extendD1 = extendD1;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getExtendD2() {
		return extendD2;
	}

	public void setExtendD2(Date extendD2) {
		this.extendD2 = extendD2;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getExtendD3() {
		return extendD3;
	}

	public void setExtendD3(Date extendD3) {
		this.extendD3 = extendD3;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}
	
//	public String getUserCode() {
//		return userCode;
//	}
//
//	public void setUserCode(String userCode) {
//		this.userCode = userCode;
//	}
	
	/**
	 * 根据类型查找上级部门
	 * @param type 机构类型    0：集团TYPE_GROUP 1：公司TYPE_COMPANY 2：部门TYPE_DEPT 3：小组TYPE_TEAM
	 * @return
	 */
	@JsonIgnore
	public Office getParentByType(String type){
		// 获取当前用户部门
		Office office = UserUtils.getOffice(getOfficeCode());
		if (office == null){
			return null;
		}
		// 查找相同类型的上级部门
		do{
			office = UserUtils.getOffice(office.getParentCode());
			if (office == null){
				return null;
			}
		}while(!type.equals(office.getType()));
		// 返回相同类型的上级部门对象
		return office;
	}

	@Override
	public String toString() {
		return officeCode;
	}

	/**
	 * 机构类型（0：集团）
	 */
	public static final String TYPE_GROUP = "0";

	/**
	 * 机构类型（1：公司）
	 */
	public static final String TYPE_COMPANY = "1";
	
	/**
	 * 机构类型（2：部门）
	 */
	public static final String TYPE_DEPT = "2";
	
	/**
	 * 机构类型（3：小组）
	 */
	public static final String TYPE_TEAM = "3";
	
}